jQuery(function($){
	var order={
			grid : null,
			init:function(){
				$("#btnSearch").on("click",function(e){
					 e.preventDefault();
					//order.pageList();
					order.grid.set({
						parms: [
								{name: "receiverName", value:$("#receiverName").val()},
							    {name: "receiverPhone", value:$("#receiverPhone").val()},
								{name: "channelOrderId", value:$("#channelOrderId").val()},
								{name: "channelOrderType", value:$("#channelOrderType").val()},
								{name: "payState", value:$("#payState").val()},
								{name: "payType", value:$("#payType").val()},
								{name: "orderStartTime", value:$("#orderStartTime").val()},
								{name: "orderEndTime", value:$("#orderEndTime").val()},
								{name: "returnOrder", value:$("#orderReturnType").val()?$("#orderReturnType").val():"1,2"},
								{name: "returnState", value:$("#returnState").val()}
			                    ],
						url: "channelOrderPageList.do"      
					});
					//order.grid.loadData();					
				});
				this.pageList();
				this.channelOrderType=[
				    {value:0,text:"京东"},
				    {value:1,text:"淘宝"},
				    {value:2,text:"天猫"}
				];
				this.orderState=[
					{value:0,text:"已提交"},
					{value:1,text:"审核通过"},
					{value:2,text:"审核不通过"},
					{value:3,text:"待支付"},
					{value:4,text:"待发货"},
					{value:5,text:"已发货"},
					{value:6,text:"已完成"},
					{value:7,text:"已取消"},
					{value:8,text:"已锁定"},
					{value:9,text:"部分发货"},
					{value:10,text:"已收货"},
					{value:11,text:"已关闭"},
					{value:12,text:"已生成包裹单"}
				];
				this.deliveryState=[
				    {value:0,text:"未发货"},
				    {value:1,text:"已发货"}              
				];
				this.payState=[
				    {value:0,text:"未支付"},
				    {value:1,text:"已支付"}              
				];
				this.payType=[
				    {value:1,text:"货到付款"},
				    {value:2,text:"邮局汇款"},
				    {value:3,text:"自提"},
				    {value:4,text:"在线支付"},   
				    {value:5,text:"公司转账"},
				    {value:6,text:"银行卡转账"} 
				];
				this.orderType=[
					{value:0,text:"白拿"},
					{value:1,text:"返还"},
					{value:2,text:"天猫"}
				];
				this.returnState=[
				    {value:0,text:"未入库"},
				    {value:1,text:"已入库"},
				    {value:2,text:"已换货"},
				    {value:3,text:"已生成入库单"},
				    {value:4,text:"待生成出库单"},
				    {value:5,text:"已生成出库单"}
				];
				this.orderReturnType=[
					{value:1,text:"退货"},
					{value:2,text:"换货"}
				];
				this.setSelectBox(this.payState,$("#payState"));
				this.setSelectBox(this.payType,$("#payType"));
				this.setSelectBox(this.channelOrderType,$("#channelOrderType"));
				this.setSelectBox(this.returnState,$("#returnState"));
				this.setSelectBox(this.orderReturnType,$("#orderReturnType"));
			},
			pageList:function(){
				order.grid = $("#maingrid").ligerGrid({
		            columns: [
		            { display: '订单编号', name: 'channelOrderId'},
		            { display: '订单渠道', name: 'channelOrderType', 
		              render: function (rowData, rowindex, value){
		            	    return order.getTextByValue(order.channelOrderType,value);
				    }},
				    /*{ display: '订单状态', name: 'orderState', 
			          render: function (rowData, rowindex, value){
			            	return order.getTextByValue(order.orderState,value);
			        }},*/
		            { display: '处理状态', name: 'returnState', 
		              render: function (rowData, rowindex, value){
		            	    if(!value){
		            		    return "未入库";
		            	    }
		            	    return order.getTextByValue(order.returnState,value);
		            }},
		            { display: '支付状态', name: 'payState',
		            	render: function (rowData, rowindex, value){
		            		return order.getTextByValue(order.payState,value);
			        }},
		            { display: '支付方式', name: 'payType',
		            	render: function (rowData, rowindex, value){
		            		if(value){
		            			//value=value.substring(0,1);
		            			return order.getTextByValue(order.payType,value);
		            		}
		            		return "";
				    }},
				    { display: '收货人/电话', name: 'receiver', width:150,
				          render: function (rowData, rowindex, value){
				            	return rowData.receiverName+"/"+rowData.receiverPhone
				    }},
		            /*{ display: '订单类型', name: 'orderType',
		            	render: function (rowData, rowindex, value){
		            		return order.getTextByValue(order.orderType,value);
				    }},*/
				    { display: '退货类型', name: 'returnOrder',
		            	render: function (rowData, rowindex, value){
		            		return order.getTextByValue(order.orderReturnType,value);
				    }},
		            { display: '总金额(元)', name: 'orderTotalPrice'},
		            { display: '下单时间',width:'160', name: 'orderStartTime'},
		            { display: '操作',width:'20%', isSort: false, render: function (rowdata, rowindex, value){
			            	var h = "<a href='javascript:showCommodityInfo(" + rowindex + ",1)'>订单信息</a> ";
			            	/*for ( var i in roles) {
			            		//商品专员,商品主管,管理员
			               		if(roles[i] == 'GOOD_PERSON' || roles[i] == 'SYSTEM_ADMIN' || roles[i] == 'GOODS_SUPERVISOR'){
			               			var css = "";
				             		if(rowdata.checkState == 1) css = "style='color: gray;'";
				             		h += "<a href='javascript:setCommodityInfo(" + rowindex + ",2)' "+css+">设置供应商</a> ";
				             	    if(rowdata.checkState != 1){
				                    	h += "<a href='javascript:updateCommodity(" + rowindex + ")' >修改</a> ";
				                    }
				                    h += "<a href='javascript:delCommodity(" + rowindex + ")' "+css+">删除</a> ";
			               		}
			        		}*/
			            	if(rowdata.returnOrder==1){
				             	if(!rowdata.returnState || rowdata.returnState==0){
				             		return "<a href='javascript:stockIn(" + rowindex + ",1)'>退货入库</a> "+h;
				             	}
			            	}
			            	if(rowdata.returnOrder==2){
			            		if(!rowdata.returnState || rowdata.returnState==0){
				             		return "<a href='javascript:stockIn(" + rowindex + ",1)'>换货入库</a> "+h;
				             	}
				             	if(rowdata.returnState==4){
				             		return "<a href='javascript:stockOut(" + rowindex + ",1)'>换货出库</a> "+h;
				             	}
			            	}
		             		return h;
		            	}
		            }
		            ], 
		            pageSize:30,
		            width: '100%',
		            height: '100%',
		            dataType: 'server', 
		            dataAction: 'server',
		            pagesizeParmName: 'pageSize',
		            parms: [
							{name: "channelOrderId", value:$("#channelOrderId").val()},
							{name: "channelOrderType", value:$("#channelOrderType").val()},
							{name: "payState", value:$("#payState").val()},
							{name: "payType", value:$("#payType").val()},
							{name: "orderStartTime", value:$("#orderStartTime").val()},
							{name: "orderEndTime", value:$("#orderEndTime").val()},
							{name: "returnOrder", value:$("#orderReturnType").val()?$("#orderReturnType").val():"1,2"},
							{name: "returnState", value:$("#returnState").val()}
		                    ],
					url: "channelOrderPageList.do",
					toolbar: toolbar
		        });
				grid=order.grid;
			},
			setSelectBox:function(objs,dom){
				if(objs  && objs.length){
					dom.html('<option value="">请选择</option>');
					for(var x in objs){
						dom.append('<option value="'+objs[x].value+'">'+objs[x].text+'</option>');
					}
				}
			},
			getTextByValue:function(objs,value){
				if(objs && objs.length){
					for(var x in objs){
						if(objs[x].value==value){
							return objs[x].text;
						}
					}
				}
			}
	};
	order.init();
}); 